package com.loanafter.fox.monitor.logback.filter;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;

import static ch.qos.logback.core.spi.FilterReply.*;

/**
 * @author jasonli@kuainiugroup.com
 * @date 2020/7/31 下午5:15
 */
public class MessagePrefixFilter extends Filter<ILoggingEvent> {

    String prefix;

    @Override
    public FilterReply decide(ILoggingEvent event) {
        if(!isStarted()){
            return NEUTRAL;
        }
        if(event.getFormattedMessage().startsWith(prefix)){
            return ACCEPT;
        } else {
            return DENY;
        }

    }

    public void start() {
        if (this.prefix != null && this.prefix.length() > 0) {
            super.start();
        }
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getPrefix() {
        return prefix;
    }
}
